

#import "WubiMainViewController.h"
#import "WubiLessonViewController.h"
#import "WBTestViewController.h"

@interface WubiMainViewController()

@end

@implementation WubiMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self) {
	}
	return self;
}

- (void)viewDidLoad
{
	[super viewDidLoad];
	APPALL.myIAPDelegate = self;
	// Custom initialization
	UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
	bgImageView.image = [UIImage imageNamed:@"wubiMainBG"];
	bgImageView.contentMode = UIViewContentModeScaleToFill;
	self.tableView.backgroundView = bgImageView;
	self.tableView.backgroundColor = [UIColor clearColor];
	self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

-(void)viewWillAppear:(BOOL)animated{
	[super viewWillAppear:animated];
	[self.tableView reloadData];
}

-(void)dealloc
{
}

- (void)didReceiveMemoryWarning
{
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	// Return the number of sections.
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	// Return the number of rows in the section.
	return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 80.0f;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell;
	static NSString *inde = @"normalwubiid";
	cell = [tableView dequeueReusableCellWithIdentifier:inde];
	if (cell == nil) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:inde];
		cell.backgroundColor = [UIColor clearColor];
		cell.detailTextLabel.numberOfLines = 0;
	}
	NSInteger nowTag = indexPath.row;
	UIImage *toImage = [UIImage imageNamed:[NSString stringWithFormat:@"wubiButton%ld",nowTag + 7]];
	cell.imageView.image = toImage;
	if(![APPALL.mydefaults boolForKey:@"hasBoughtLevel"] && nowTag > 0){
		cell.textLabel.textColor = [UIColor lightGrayColor];
		cell.detailTextLabel.textColor = [UIColor lightGrayColor];
		cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
		cell.detailTextLabel.highlightedTextColor = [UIColor lightGrayColor];
	}else{
		cell.textLabel.textColor = [UIColor blackColor];
		cell.detailTextLabel.textColor = [UIColor blackColor];
		cell.textLabel.highlightedTextColor = [UIColor blackColor];
		cell.detailTextLabel.highlightedTextColor = [UIColor blackColor];
	}
	switch (nowTag) {
		case 0:
			cell.textLabel.text = @"字根练习";
			cell.detailTextLabel.text = @"试试看你能不能记住所有的字根的位置吧，不准看小抄。";
			break;
		case 1:
			cell.textLabel.text = @"单字练习";
			cell.detailTextLabel.text = @"字根背完了，尝试下输入单字吧！(本测试采用标准4码输入，无简码)";
			break;
		case 2:
			cell.textLabel.text = @"词语练习";
			cell.detailTextLabel.text = @"在能够熟练打出单字的基础上，使用词语输入来加快输入速度吧！";
			break;
		case 3:
			cell.textLabel.text = @"自由练习";
			cell.detailTextLabel.text = @"没有任何时间限制的单字练习，允许轻松自在的进行思考！";
			break;
		case 4:
			cell.textLabel.text = @"综合练习";
			cell.detailTextLabel.text = @"单字和词语综合练习，与时间赛跑，并与世界竞争证明自己的实力！";
			break;
		default:
			break;
	}
	return cell;
}

-(void)openAllLevel
{
	UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"开启全部练习" message:@"练习功能仅需6元终生使用，确认购买吗？" preferredStyle:UIAlertControllerStyleAlert];
	//    vc.view.backgroundColor = [UIColor goldColor];
	UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
	UIAlertAction *ok1Action = [UIAlertAction actionWithTitle:@"我要购买" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
		[APPALL startToIAP:kIAPOpenLevel];
	}];
	UIAlertAction *ok8Action = [UIAlertAction actionWithTitle:@"买过了，恢复购买" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
		[APPALL startToRestore:kIAPOpenLevel];
	}];
	[vc addAction:ok1Action];
	[vc addAction:ok8Action];
	[vc addAction:cancelAction];
	[self presentViewController:vc animated:NO completion:nil];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	switch (indexPath.row) {
		case 0:{
			WBTestViewController *vc = [[WBTestViewController alloc] init];
			vc.myTag = ETestZigen;
			[self.navigationController pushViewController:vc animated:YES];
		}
			break;
		case 1:
			if(![APPALL.mydefaults boolForKey:@"hasBoughtLevel"]) {
				[self openAllLevel];
			}else{
				WBTestViewController *vc = [[WBTestViewController alloc] init];
				vc.myTag = ETestZi;
				[self.navigationController pushViewController:vc animated:YES];
			}
			break;
		case 2:
			if(![APPALL.mydefaults boolForKey:@"hasBoughtLevel"]) {
				[self openAllLevel];
			}else{
				WBTestViewController *vc = [[WBTestViewController alloc] init];
				vc.myTag = ETestCiyu;
				[self.navigationController pushViewController:vc animated:YES];
			}
			break;
		case 3:
			if(![APPALL.mydefaults boolForKey:@"hasBoughtLevel"]) {
				[self openAllLevel];
			}else{
				WBTestViewController *vc = [[WBTestViewController alloc] init];
				vc.myTag = ETestTimeless;
				[self.navigationController pushViewController:vc animated:YES];
			}
			break;
		case 4:
			if(![APPALL.mydefaults boolForKey:@"hasBoughtLevel"]) {
				[self openAllLevel];
			}else{
				WBTestViewController *vc = [[WBTestViewController alloc] init];
				vc.myTag = ETestAll;
				[self.navigationController pushViewController:vc animated:YES];
			}
			break;
		default:
			break;
	}
}

-(void)VCIAPSucceed:(NSString *)aSucc{
	if ([aSucc length] > 0) {
		// 向自己的服务器验证购买凭证
		[APPALL.mydefaults setBool:true forKey:@"hasBoughtLevel"];
		[self.tableView reloadData];
	}
	
}

-(void)VCIAPFailed:(NSString *)aSucc{
	UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"内购失败" message:aSucc preferredStyle:UIAlertControllerStyleAlert];
	//    vc.view.backgroundColor = [UIColor goldColor];
	UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
	[vc addAction:cancelAction];
	[self presentViewController:vc animated:NO completion:nil];
}

-(void)rightPressed:(id)sender{
	[self openAllLevel];
}

@end
